package com.woniu.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniu.entity.NuserSubscription;
import com.woniu.vo.UserOpenidVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @Description:
 * @Author: woniu
 * @date: 2024/1/10 9:26
 */
@Mapper
public interface NuserSubscriptionMapper extends BaseMapper<NuserSubscription> {
    @Select("SELECT nu.id AS nuser_id,nu.openid FROM n_user_subscription AS nus LEFT JOIN n_user AS nu ON nu.id = nus.nuser_id LEFT JOIN wechat_template_message AS wtm ON wtm.id = nus.template_id  WHERE nus.template_id = #{id} ORDER BY nus.create_time DESC")
    List<UserOpenidVO> findSubUserByTempId(@Param("id") Integer id);

    @Select("SELECT nu.id AS nuser_id,nu.openid FROM n_user_subscription AS nus LEFT JOIN n_user AS nu ON nu.id = nus.nuser_id LEFT JOIN red_packet_pool AS rpp ON rpp.receive_nuser_id = nus.nuser_id WHERE nus.template_id = #{id} AND nus.nuser_id NOT IN (SELECT rpp.receive_nuser_id FROM red_packet_pool AS rpp WHERE rpp.create_time BETWEEN #{startTime} AND #{endTime} AND rpp.already_received = 1) ORDER BY nus.create_time DESC")
    List<UserOpenidVO> findSubUserAndNoRedPacketByTempId(@Param("id") Integer id,@Param("startTime") String startTime, @Param("endTime") String endTime);

}
